package classtrait

/**
 * 特质之 可叠加修改
 *
 * @author tzp
 * @since 2021/1/16
 */
abstract class IntQueue {
  def get(): Int

  def put(x: Int)
}

trait Doubling extends IntQueue {
  abstract override def put(x: Int): Unit = {
    super.put(x * 2)
  }
}

trait Filtering extends IntQueue {
  abstract override def put(x: Int): Unit = {
    if (x > 0) super.put(x)
  }
}

import scala.collection.mutable.ArrayBuffer

class BasicIntQueue extends IntQueue /*with Doubling*/ { //思考这一级withDoubling为啥不行
  private val buf = new ArrayBuffer[Int]()

  override def get(): Int = buf.remove(0)

  override def put(x: Int): Unit = buf += x
}

class MyQueue extends BasicIntQueue with Doubling

object IntQ {
  def main(args: Array[String]): Unit = {
    val x = new MyQueue
    x.put(10)
    println(x.get())

    val y = new BasicIntQueue with Filtering
    y.put(1)
    y.put(-1)
    println(y.get())
    println(y.get())
  }
}